package com.jackrain.nea.fc.cp.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jackrain.nea.core.db.Tools;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.cp.api.ViewHrJobSaveCmd;
import com.jackrain.nea.fc.cp.mapper.ViewHrJobMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.face.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * HR与FC人员接口-04.职务
 *
 * @author WSL
 * @create 2017/12/14
 */
@Slf4j
@Component
@Service(protocol = "dubbo" , validation = "true" , version = "1.0" , group = "cp")
public class ViewHrJobCmdImpl extends CommandAdapter implements ViewHrJobSaveCmd{

    @Override
    public ValueHolder execute(HashMap map) throws NDSException{
        ValueHolder vh = new ValueHolder();
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"), "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        log.info("ViewHrJobCmdImpl--param-->{}",param);

        if (null == param || param.size() == 0) {
            throw new NDSException("参数格式错误！");
        }

        // Mapper
        ViewHrJobMapper viewHrJobMapper = ApplicationContextHandle.getBean(ViewHrJobMapper.class);

        JSONArray paramArray = param.getJSONArray("data");

        User user = Tools.getUser("INTER99999");
        // 判断FC中表【CP_C_JOB】是否存在HR005_ID=pk_job
        for (int i = 0; i < paramArray.size(); i++) {
            JSONObject paramJson = paramArray.getJSONObject(i);
            JSONObject checkJson = viewHrJobMapper.checkEmp(paramJson.getString("HR005_ID"));
            if (checkJson.getInteger("COUNT") > 0){
                paramJson.put("AD_CLIENT_ID",37);
                paramJson.put("AD_ORG_ID",27);
                // 修改人
                paramJson.put("MODIFIERNAME", user.getName());
                paramJson.put("MODIFIERENAME", user.getEname());
                paramJson.put("MODIFIERID", user.getId().longValue());
                paramJson.put("ID",checkJson.getLong("ID"));
                // 更新
                viewHrJobMapper.update(paramJson);
            }else{
                // 新增时  ISACTIVE(固定为Y)
                paramJson.put("ISACTIVE","Y");
                paramJson.put("AD_CLIENT_ID",37);
                paramJson.put("AD_ORG_ID",27);
                // 创建人
                paramJson.put("OWNERID", user.getId().longValue());
                paramJson.put("OWNERNAME", user.getName());
                paramJson.put("OWNERENAME", user.getEname());
                // 修改人
                paramJson.put("MODIFIERNAME", user.getName());
                paramJson.put("MODIFIERENAME", user.getEname());
                paramJson.put("MODIFIERID", user.getId().longValue());
                // 创建时间
                paramJson.put("CREATIONDATE", paramJson.getString("MODIFIEDDATE"));

                paramJson.put("ID", Tools.getSequence("CP_C_JOB"));
                // 新增
                viewHrJobMapper.insert(paramJson);
            }
        }
        return vh;
    }
}
